package model

import (
	"context"
	"database/sql"
	"fmt"

	"github.com/Masterminds/squirrel"
	"github.com/jinzhu/copier"
	"github.com/zeromicro/go-zero/core/stores/cache"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

var _ UmsRoleModel = (*customUmsRoleModel)(nil)

type (
	// UmsRoleModel is an interface to be customized, add more methods here,
	// and implement the added methods in customUmsRoleModel.
	UmsRoleModel interface {
		umsRoleModel
		Trans(ctx context.Context, fn func(ctx context.Context, session sqlx.Session) error) error
		RowBuilder() squirrel.SelectBuilder
		CountBuilder(field string) squirrel.SelectBuilder
		DeleteBatch(ctx context.Context, ids ...int64) error
		FindAll(ctx context.Context) ([]*UmsRole, error)
		FindByPage(ctx context.Context, rowBuilder squirrel.SelectBuilder, pageNum, pageSize int64) ([]*UmsRole, error)
		FindCount(ctx context.Context, countBuilder squirrel.SelectBuilder) (int64, error)
	}

	customUmsRoleModel struct {
		*defaultUmsRoleModel
	}
)

// NewUmsRoleModel returns a model for the database table.
func NewUmsRoleModel(conn sqlx.SqlConn, c cache.CacheConf) UmsRoleModel {
	return &customUmsRoleModel{
		defaultUmsRoleModel: newUmsRoleModel(conn, c),
	}
}
func (m *defaultUmsRoleModel) DeleteBatch(ctx context.Context, ids ...int64) error {
	umsRoleIdKey := fmt.Sprintf("%s%v", cacheUmsRoleIdPrefix, ids)
	var data []interface{}
	err := copier.Copy(&data, ids)
	if err != nil {
		return err
	}
	_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
		query := fmt.Sprintf("delete from %s where `id` in(?)", m.table)
		return conn.ExecCtx(ctx, query, data...)
	}, umsRoleIdKey)
	return err
}
func (m *defaultUmsRoleModel) FindAll(ctx context.Context) ([]*UmsRole, error) {
	rowBuilder := m.RowBuilder()
	query, values, err := rowBuilder.ToSql()
	if err != nil {
		return nil, err
	}
	var resp []*UmsRole
	err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (m *defaultUmsRoleModel) FindByPage(ctx context.Context, rowBuilder squirrel.SelectBuilder, pageNum, pageSize int64) ([]*UmsRole, error) {
	if pageSize == 0 {
		pageSize = 10
	}
	if pageNum == 0 {
		pageNum = 1
	}
	query, values, err := rowBuilder.Offset(uint64((pageNum - 1) * pageSize)).Limit(uint64(pageSize)).ToSql()
	if err != nil {
		return nil, err
	}
	var resp []*UmsRole
	err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...)
	if err != nil {
		return nil, err
	}
	return resp, nil
}
func (m *defaultUmsRoleModel) FindCount(ctx context.Context, countBuilder squirrel.SelectBuilder) (int64, error) {
	query, values, err := countBuilder.ToSql()
	if err != nil {
		return 0, err
	}
	var resp int64
	err = m.QueryRowNoCacheCtx(ctx, &resp, query, values...)
	if err != nil {
		return 0, err
	}
	return resp, nil
}

// export logic
func (m *defaultUmsRoleModel) Trans(ctx context.Context, fn func(ctx context.Context, session sqlx.Session) error) error {
	return m.TransactCtx(ctx, func(ctx context.Context, session sqlx.Session) error {
		return fn(ctx, session)
	})

}

// export logic
func (m *defaultUmsRoleModel) RowBuilder() squirrel.SelectBuilder {
	return squirrel.Select(umsRoleRows).From(m.table)
}

// export logic
func (m *defaultUmsRoleModel) CountBuilder(field string) squirrel.SelectBuilder {
	return squirrel.Select("COUNT(" + field + ")").From(m.table)
}

// export logic
func (m *defaultUmsRoleModel) SumBuilder(field string) squirrel.SelectBuilder {
	return squirrel.Select("IFNULL(SUM(" + field + "),0)").From(m.table)
}
